/*
 * @Author: Marte
 * @Date:   2020-07-07 13:24:38
 * @Last Modified by:   Marte
 * @Last Modified time: 2020-07-08 15:14:45
 */

'use strict';
import Vue from "vue";
import Vuex from "vuex";
import tabRoutes from "@/router/tabRoutes";
Vue.use(Vuex);

let paths = [];
tabRoutes.forEach(item => paths.push(item.name));
const ADD_COUNT = 'ADD_COUNT'; // 用常量代替事件类型，使得代码更清晰
const REMOVE_COUNT = 'REMOVE_COUNT';
const ADD_RZ = 'ADD_RZ';
const REMOVE_RZ = 'REMOVE_RZ';
const ADD_SEARCH = 'ADD_SEARCH'
const REMOVE_SEARCH = 'REMOVE_SEARCH'
const ADD_DISCOUNT = 'ADD_DISCOUNT'
const REMOVE_DISCOUNT = 'REMOVE_DISCOUNT'
const ADD_CATEGORY = 'ADD_CATEGORY'
const REMOVE_CATEGORY = 'REMOVE_CATEGORY'
const KEEPALIVE = 'KEEPALIVE'
const [home, zk_sy, mf_sy, invite, my] = paths;
const state = {
    uid: localStorage.getItem('uid'),
    token: JSON.parse(localStorage.getItem('token')),
    aeskey: localStorage.getItem('aeskey'),
    username: localStorage.getItem('username'),
    isRz: false,
    s_top: localStorage.getItem('s_top'),
    param: localStorage.getItem('param'),
    discount_history: localStorage.getItem("discount_history") ? JSON.parse(localStorage.getItem("discount_history")) : [],
    search_history: localStorage.getItem("search_history") ? JSON.parse(localStorage.getItem("search_history")) : [],
    category: localStorage.getItem("category") ? JSON.parse(localStorage.getItem("category")) : [],
    tabList: [{
            title: '首页',
            name: home,
            path: '/home',
            icon: {
                inactive: require('@/assets/index/icon_sy.png'),
                active: require('@/assets/index/icon_sy_active.png'),
            }
        },
        // {
        //     title: '折扣',
        //     name: zk_sy,
        //     path: { path: '/discount_sy', query: { typeId: 2, categoryId: '', sortId: '', title: '折扣', cate_name: '', index: -1, downtypeId: '', puttypeId: '', sortId: '' } },
        //     icon: {
        //         inactive: require('@/assets/index/icon_zk.png'),
        //         active: require('@/assets/index/icon_zk_active.png'),
        //     }
        // },
        {
            title: '优惠卷',
            name: mf_sy,
            path: { path: '/nine_nine', query: { from: "home", active: 0, type: 'nine'} },
            icon: {
                inactive: require('@/assets/index/icon_sy_.png'),
                active: require('@/assets/index/icon_sy__active.png'),
            }
        },
        // {
        //     title: '优惠卷',
        //     name: mf_sy,
        //     path: { path: '/free_sy', query: { typeId: 1, categoryId: '', sortId: '', title: '零元购', cate_name: '', index: -1, downtypeId: '', puttypeId: '', sortId: '' } },
        //     icon: {
        //         inactive: require('@/assets/index/icon_sy_.png'),
        //         active: require('@/assets/index/icon_sy__active.png'),
        //     }
        // },
        // {
        //     title: '邀请',
        //     name: invite,
        //     path: '/invite',
        //     icon: {
        //         inactive: require('@/assets/index/icon_yq.png'),
        //         active: require('@/assets/index/icon_yq_active.png'),
        //     }
        // },
        {
            title: '我的',
            name: my,
            path: '/my',
            icon: {
                inactive: require('@/assets/index/icon_wd.png'),
                active: require('@/assets/index/icon_wd_active.png'),
            }
        }
    ]
};
const mutations = {
    //写法与getters相类似
    //组件想要对于vuex 中的数据进行的处理
    //组件中采用this.$store.commit('方法名') 的方式调用，实现充分解耦
    //内部操作必须在此刻完成(同步)
    [ADD_COUNT](state, param) { // 第一个参数为 state 用于变更状态 登录
        localStorage.setItem("token", JSON.stringify(param.token));
        localStorage.setItem("uid", param.uid);
        localStorage.setItem('aeskey', param.aeskey)
        localStorage.setItem('username', param.username)
        state.token = param.token;
        state.uid = param.uid;
        state.aeskey = param.aeskey;
        state.username = param.username;
    },
    [REMOVE_COUNT](state, param) { // 退出登录
        localStorage.removeItem("token", param.token);
        localStorage.removeItem("uid", param.uid);
        localStorage.removeItem('aeskey', param.aeskey)
        localStorage.removeItem('username', param.username)
        state.token = param.token;
        state.uid = param.uid;
        state.aeskey = param.aeskey;
        state.username = param.username;
        console.log(state.uid, 'uid')
    },
    [ADD_RZ](state, param) {
        localStorage.setItem("isRz", param);
        state.isRz = param;
    },
    [REMOVE_RZ](state, param) {
        localStorage.setItem("isRz", param);
        state.isRz = param;
    },
    [ADD_SEARCH](state, param) {
        state.search_history = param;
        localStorage.setItem('search_history', JSON.stringify(param));
    },
    [REMOVE_SEARCH](state, param) {
        state.search_history = param;
        localStorage.setItem('search_history', JSON.stringify(param));
    },
    [ADD_DISCOUNT](state, param) {
        console.log(param)
        state.discount_history = param;
        localStorage.setItem('discount_history', JSON.stringify(param));
    },
    [REMOVE_DISCOUNT](state, param) {
        state.discount_history = param;
        localStorage.setItem('discount_history', JSON.stringify(param));
    },
    [ADD_CATEGORY](state, param) {
        state.category = param;
        localStorage.setItem('category', JSON.stringify(param));
    },
    [REMOVE_CATEGORY](state, param) {
        state.category = param;
        localStorage.setItem('category', JSON.stringify(param));
    },
    [KEEPALIVE](state, param) {
        state.s_top = param.s_top
        state.param = param.param;
    }
};
const actions = {};
const getters = {
    tabList: state => state.tabList,
    isRz: state => state.isRz,
    token: state => state.token,
    uid: state => state.uid,
    aeskey: state => state.aeskey,
    username: state => state.username,
    s_top: state => state.s_top,
    param: state => state.param
};
const store = new Vuex.Store({
    state,
    getters,
    mutations
});
export default store;